#!/bin/sh
#
# Copyright (c) 2005 Atheme Development Group
# Rights to this code are documented in doc/LICENSE.
#
# This file is a simple setup script.
#
# $Id: setup 7871 2007-03-06 01:24:00Z nenolod $

PREFIX="$HOME/atheme"
LARGENET="no"
NLS="yes"
SASL="yes"
CONTRIB="no"
VERSION=`grep AC_INIT configure.ac | awk '{print \$2}' | sed s:\,::g`

if [ "x$PAGER" = "x" ]; then
	PAGER="more"
fi

export ok INPUT

clear

cat .logo
echo "                     version $VERSION"

echo ""

if [ -f NEWS ] ; then
  ok=0
  echo "Would you like to read the release notes (recommended)?"
  while [ $ok -eq 0 ] ; do
    echo -n "[Yes] "
    if read INPUT ; then : ; else echo "" ; exit 1 ; fi
      if [ ! "$INPUT" ] ; then
        echo ""
        $PAGER NEWS
        echo ""
        echo "-----"
        ok=1
      fi
      case $INPUT in
        [Yy]*)
          echo ""
          $PAGER NEWS
          ok=1
          ;;
        [Nn]*)
          ok=1
          ;;
      esac
  done
  echo ""
fi

ok=0
echo "Where do you want Atheme to be installed?"
echo "Press enter for the default, or type a new destination."
while [ $ok -eq 0 ] ; do
  echo -n "[$PREFIX] "
  if read INPUT ; then : ; else echo "" ; exit 1 ; fi
  if [ ! "$INPUT" ] ; then
    INPUT=$PREFIX
  fi
  ok=1
  if [ -r "$INPUT/src/atheme.c" ]; then
    echo "That looks like an Atheme source directory."
    echo "Please choose another."
    ok=0
  fi
done
PREFIX=$INPUT

echo ""

ok=0
echo "Are you running a large network (more than 2000 users)?"
while [ $ok -eq 0 ] ; do
  echo -n "[$LARGENET] "
  if read INPUT ; then : ; else echo "" ; exit 1 ; fi
  if [ ! "$INPUT" ] ; then
    INPUT=$LARGENET
    ok=1
  else
    ok=1
  fi
done
LARGENET=$INPUT

ARGS="--prefix=$PREFIX"

case "$LARGENET" in
  [Yy]*)
    ARGS="$ARGS --enable-large-net"
    ;;
esac

echo ""

ok=0
echo "Would you like to disable support for internationalization of"
echo "the messages used by Services? Internationalization support"
echo "will require gettext to be installed, including development packages."
while [ $ok -eq 0 ] ; do
  echo -n "[$NLS] "
  if read INPUT ; then : ; else echo "" ; exit 1 ; fi
  if [ ! "$INPUT" ] ; then
    INPUT=$NLS
    ok=1
  else
    ok=1
  fi
done
NLS=$INPUT

case "$NLS" in
  [Yy]*)
    ARGS="$ARGS --disable-nls"
    ;;
esac

echo ""

ok=0
echo "Would you like to compile the modules in the contrib directory?"
echo "This contains modules that may be useful."
echo "There is no support for them and no guarantee that they will work."
while [ $ok -eq 0 ] ; do
	echo -n "[$CONTRIB] "
	if read INPUT ; then : ; else echo "" ; exit 1 ; fi
	if [ ! "$INPUT" ] ; then
		INPUT=$CONTRIB
		ok=1
	  else
		ok=1
	  fi
done
CONTRIB=$INPUT

# Yeah, I know sed and escaping everything is probably not the cleanest way to do this, but it works...
case "$CONTRIB" in
	[Yy]*)
          cp Makefile Makefile.bak
	  sed 's/$(PODIR)/contrib &/' Makefile.bak >Makefile
	  ;;
esac

echo ""

ok=0
echo "Would you like to pass any additional options to configure? If so,"
echo "place them here:"
while [ $ok -eq 0 ] ; do
  echo -n "[no] "
  if read INPUT ; then : ; else echo "" ; exit 1 ; fi
  if [ ! "$INPUT" ] || [ "$INPUT" = "no" ] ; then
    INPUT=""
    ok=1
  else
    ok=1
  fi
done
ARGS="$ARGS $INPUT"

if [ -f Makefile ]; then
	make distclean
fi
if [ ! -f configure ]; then sh autogen.sh; fi
sh configure $ARGS
if [ $? != 0 ]; then
	echo "Configure failed"
	echo "Command was: sh configure $ARGS"
	echo "Please fix this and try again"
	exit 1
fi

echo ""
echo "Press enter to begin compilation."
echo -n ""
if read INPUT ; then : ; else echo "" ; exit 1 ; fi

if make --version 2>/dev/null | grep 'GNU Make' >/dev/null; then
	GMAKE=make
elif gmake --version 2>/dev/null | grep 'GNU Make' >/dev/null; then
	GMAKE=gmake
else
	echo "'make' does not seem to be GNU Make, trying to use it anyway..."
	GMAKE=make
fi

$GMAKE
if [ $? != 0 ]; then
	echo "Compilation failed"
	echo "Command was: $GMAKE"
	echo "Please fix this and try again"
	exit 1
fi

echo ""
echo "Press enter to begin installation."
echo -n ""
if read INPUT ; then : ; else echo "" ; exit 1 ; fi

$GMAKE install
if [ $? != 0 ]; then
	echo "Installation failed"
	echo "Command was: $GMAKE install"
	echo "Please fix this and try again"
	exit 1
fi

echo "----------------------------------------------------------------"
echo ">>> Atheme setup completed on `date`"
echo "----------------------------------------------------------------"

exit 0
